package iotwisp.base.video;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.stereotype.Component;

@Component
public class VideoCache {

	private Map<Integer, LinkedBlockingQueue<byte[]>> streams = new HashMap<Integer, LinkedBlockingQueue<byte[]>>();

	public void addData(int seq, byte[] data, int length) {
		if (streams.get(seq) == null) {
			LinkedBlockingQueue<byte[]> queue = new LinkedBlockingQueue<>();
			streams.put(seq, queue);
		}

		streams.get(seq).add(data);
	}

	public byte[] pollData(int seq) throws InterruptedException {
		if (streams.get(seq) == null) {
			return null;
		}

		return streams.get(seq).take();
	}
}
